/**
 * Created by Administrator on 2017/12/5.
 */
$(function(){
    var containArr = ['包含','大于','小于','大于等于','小于等于','是空的'];
    var holidayArr = ['HOLIDAY','YHOLIDAY','MHOLIDAY','ORG_ID','ROWID','[Additional Filter]'];
    var andArr = ['and','or','and not','or not'];

    $('.field_tools').on("click",'a',function () {
        if($(this).hasClass('field_toolsAdd')){
            $('.field_tools').css({'position':'absolute','bottom':'0','left':'0','background':'#fff'});
            var fieldTemplate = '<div class="field_member">' +
                '<input type="checkbox" checked="checked"/> ' +
                '<div class="HOLIDAYBox"><a href="javascript:;" class="HOLIDAY currentField">HOLIDAY</a> ' +
                '</div>' +
                '<div class="containBox"><a href="javascript:;" class="contain currentField">包含</a></div>' +
                '<div class="QMarkBox"><a href="javascript:;" class="QMark currentField">&lt;?&gt;</a></div>' +
                '<div class="f_andBox"><a href="javascript:;" class="f_and currentField">and</a></div>' +
                '<a class="fa fa-close close_field"></a>'+
                '</div>';
            var field_items =  $('.field_items');
            field_items.append(fieldTemplate);
            $('.field_member:last').addClass('active_gary').siblings().removeClass('active_gary');

        }else if ($(this).hasClass('field_toolsUp')){
            $('.active_gary').insertBefore($('.active_gary').prev())
        }else if ($(this).hasClass('field_toolsDown')){
            $('.active_gary').insertAfter($('.active_gary').next())
        }
    });

    $('.selectData_filter').on('click','.field_member',function () {
        $(this).addClass('active_gary').siblings().removeClass('active_gary');
    });


    $('.field_items').on('click','a',function (e) {
        var that = $(this);
        if($(this).hasClass('close_field')) {
            if($(this).parent().hasClass('active_gary')){
                if($(this).parent().prev().length > 0){
                    $(this).parent().prev().addClass('active_gary')
                }else {
                    $(this).parent().next().addClass('active_gary')
                }
            }
            $(this).parent().remove();
        }else if($(this).hasClass('HOLIDAY')){
            listChoose(that,holidayArr,$('.selectData'),e)
        }else if($(this).hasClass('contain')){
            listChoose(that,containArr,$('.selectData'),e)
        }else if($(this).hasClass('f_and')){
            listChoose(that,andArr,$('.selectData'),e)
        }else if($(this).hasClass('QMark')){
            if($('.item_ul').length >0){    //如果有就删除list
                $('.item_ul').remove();
            }

            if(that.hasClass('openList')){      //如果this已打开list
                $('.openList').removeClass('openList'); //删除
                //  that.removeClass('openList')
            }else {
                $('.openList').removeClass('openList');
                that.addClass('openList');
                var e = e || window.e;
                $('.selectData').append('<div class="QMarkList item_ul"><input type="text"></div>');
                $('.QMarkList').css({'left':''+(e.clientX+5)+'px','top':''+(e.clientY+10)+'px'});

                $('.QMarkList input').bind('keypress',function(e){  //input框赋值

                    if(e.keyCode === 13){
                        var val =  Trim($(this).val(),'g') ;
                        if(val !== ''){
                            that.text(val);
                            $('.openList').removeClass('openList');
                            $('.item_ul').remove();
                        }
                    }
                })
            }

        }

    });


    $(document).click(function (e) {
        if(!$(e.target).hasClass('currentField')){
            $('.openList').removeClass('openList');
            $('.item_ul').remove();
        }

    });

    function listChoose(that,liArr,parentEle,e) {
        if($('.item_ul').length >0){    //如果有就删除list
            $('.item_ul').remove();
        }

        if(that.hasClass('openList')){      //如果this已打开list
            $('.openList').removeClass('openList'); //删除
          //  that.removeClass('openList')
        }else {
            $('.openList').removeClass('openList');
            that.addClass('openList');
            var fa_item_ul = $('<ul class="vakata-context jstree-contextmenu jstree-default-contextmenu item_ul"></ul>');
            /* var  fa_item_li = ['打开','浏览数据','SQL','创建交换流程（以本对象为输入）','创建交换流程（以本对象为输出'];  //一级的li*/
            var e = e || window.e;

            $.each(liArr,function (i, v) {
                fa_item_ul.append('<li class=""><a class="" href="#">'+v+'</a></li>')
            });

            fa_item_ul.appendTo(parentEle);
            fa_item_ul.css({'display':'block','position':'absolute','z-index':'1000','background':'#fff','left':''+(e.clientX+5)+'px','top':''+(e.clientY+10)+'px'});

            $('.item_ul').on('click','a',function () {

                $('.openList').removeClass('openList'); //list已关闭

                that.text($(this).text());  //list的中的数据加到that上
                $('.item_ul').remove();        //删除list
            })
        }
        

    }

    //字符串去空格
    function Trim(str,is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g,"");
        if(is_global.toLowerCase()=="g")
        {
            result = result.replace(/\s/g,"");
        }
        return result;
    }


});